package com.github.livebase.util.base;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ObjectsUtil
 * @description:
 * @author: biao532
 * @create: 2023-11-24 00:19
 * @Version 1.0
 **/
public class ObjectsUtil {
    public static class Empty {
        public static final Map MAP = Collections.unmodifiableMap(new HashMap<>(0));
        public static final List LIST = Collections.unmodifiableList(new ArrayList<>(0));
        public static final String STRING = "";
        public static final String[] STRINGS = new String[0];

        public static final InputStream INPUTSTREAM = new InputStream() {
            @Override
            public int read() throws IOException {
                return -1;
            }
        };
    }
    @FunctionalInterface
    public interface Converter<T, V> {
        V convert(T t);
    }
    public static <T, V> V convert(T data, Converter<T, V> converter) {
        return converter.convert(data);
    }

    public static <T> T getOrDefault(T chk, T data) {
        if (chk == null) {
            return data;
        }
        return chk;
    }

    public static boolean safeEquals(Object obj, Object obj2) {
        if (obj == obj2) {
            return true;
        }
        if (obj == null) {
            return obj2 == null;
        }
        return obj.equals(obj2);
    }
}
